package thread;

/**
 * 守护线程
 * 守护线程是通过普通线程（我们真长创建的线程，普通线程也称为前台线程，用户线程）调用方法setDaemon(true)
 * 设置转换
 */
public class DaemonThreadDemo {
    public static void main(String[] args) {
        Thread rose = new Thread("Rose"){
            public void run() {
                for (int i = 0; i < 5; i++) {
                    System.out.println(getName() + ":let me go!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(getName() + ":啊啊啊啊啊");
                System.out.println("扑通");
            }
        };

        Thread jack = new Thread("Jack") {
            public void run() {
                while (true) {
                    System.out.println(getName() + ":You jump, I jump too!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        rose.start();
        jack.setDaemon(true);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        jack.start();
    }
}
